<?php
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Schema\View;
class documents extends BaseCtrl{
	function __construct()
	{
		parent::__construct();
		$this->load->library('jsUtils');
	}
	
	
	public function index(){
		$this->load->view("v_header");
		//$this->loadalldocument();
		//$this->loaddocumentbytitle("test");
		echo "<hr>";
		$this->searchdocument();
	}
	
	
	
	public function refresh(){
		
	}
	
	
	//charge tout les documents
	public function loadalldocument(){
		$query = $this->doctrine->em->createQuery("SELECT d FROM Document d");
		$document = $query->getResult();
		$this->load->view('v_affichageDocument',array('documents'=>$document));
	}
	
	
	//charge le ou les document(s) par titre
	public function loaddocumentbytitle($titre){
		if($titre == "BtnRecherche"){
			$titre = $_POST['titre'];
			if ($titre == ""){
				$this->index();
			}
		}
		$query = $this->doctrine->em->createQuery("SELECT d FROM Document d WHERE d.titre LIKE :title ");
		$query->setParameter('title', '%'.$titre.'%');
		$document = $query->getResult();
		if(empty($document)){		
			$this->index();
			
		}
		else{
			$this->load->view('v_document',array('documents'=>$document));
		}
	}

					
	
	
	
	//recherche de(s) document(s)
	public function searchdocument(){
		
		$this->jsutils->postFormAndBindTo("#BtnRecherche","click", "loaddocumentbytitle","frmSearch","body");
		$this->jsutils->compile();
		$this->load->view('v_rechercheDocument');

	}
	
	////////////////////////////////////////////////////////////THEO/////////////////////////////////////////////////////////////////
	
	
	
	public function mesDocs(){
		$this->load->view('v_header');
		$this->load->view('v_arbo', array('domaines'=>$this->_loadDomaineByMonde(1)));
	}
	
	public function _loadDocuments(){
		$query = $this->doctrine->em->createQuery("SELECT d FROM Document d");
		return $query->getResult();
	}
	
	// marche pas
	public function _loadDocumentsByDomaine($idDomaine){
		$query = $this->doctrine->em->createQuery("SELECT d FROM Document d WHERE d.theme_id LIKE :idtheme AND ");
		$query->setParameter('idtheme', $idDomaine);
		return $query->getResult();
	}
	
	
	public function _loadDocumentsByTheme($idtheme){
		$query = $this->doctrine->em->createQuery("SELECT d FROM Document d WHERE d.theme_id='".$idtheme."'");
		return $query->getResult();
	}
	
	public function _loadThemeByDomaine($iddomaine){
		$query = $this->doctrine->em->createQuery("SELECT t FROM Theme t WHERE t.domaine_id='".$iddomaine."';");
		return $query->getResult();
	}
	
	public function _loadDomaineByMonde($idmonde){
		$query = $this->doctrine->em->createQuery("SELECT d FROM Domaine d WHERE d.monde_id='".$idmonde."';");
		return  $query->getResult();
	}
	
	public function _loadThemeByTheme($idtheme){
		$query = $this->doctrine->em->createQuery("SELECT t FROM Theme t WHERE t.theme_id='".$idtheme."';");
		return $query->getResult();
	}
	
}
	
	
	
	